package com.youlu.campus.common.exception;


import com.youlu.campus.common.base.enums.CodeEnum;

import java.util.Objects;

/**
 * 业务异常
 */
public class BusinessException extends RuntimeException {

    private ApiErrorCode error;

    private Integer code;

    /**
     * 无参数自定义业务异常构造方法.
     */
    public BusinessException() {

    }

    public BusinessException(ApiErrorCode error) {
        this.error = error;
        this.code = error.getCode();

    }

    /**
     * @param message 异常消息.
     */
    public BusinessException(String message) {
        super(message);
    }

    /**
     * @param message 异常消息.
     */
    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    /**
     * @param cause 根异常.
     */
    public BusinessException(Throwable cause) {
        super(cause);
    }

    /**
     * @param message 异常消息.
     * @param cause   根异常.
     */
    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }


    public ApiErrorCode getError() {
        if (Objects.isNull(error)) {
            this.error = CodeEnum.FAIL;
        }
        return this.error;
    }

    public int getCode() {
        if (this.code == null) {
            this.code = CodeEnum.FAIL.getCode();
        }
        return this.code;
    }
}
